package framework.mybatis.annotation2;

import framework.mybatis.annotation2.entity.User;
import framework.mybatis.annotation2.mapper.UserMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Assert;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Reader;

/**
 * 基于 接口方法的 调用，namespace 设置为接口的全限定类名就可以自动（根据方法名）绑定
 *
 * @author ME
 * @date 2021/4/29
 */
public class AnnotationConfigTest2 {

    private static final Logger LOG = LoggerFactory.getLogger(AnnotationConfigTest2.class);

    private static SqlSessionFactory sqlSessionFactory;

    @Test
    public void test() {
        try (SqlSession session = sqlSessionFactory.openSession()) {
            UserMapper mapper = session.getMapper(UserMapper.class);

            User u1 = new User(999);
            mapper.delUser(u1);

            User user = new User(999, "Lucy", 14);
            int result = mapper.addUser(user);
            Assert.assertEquals("插入 1行",
                    1, result);
            LOG.info("insert User[999]: {}", mapper.getUser(user));
            LOG.info("UserVO: {}", mapper.getUserVO(user));

            user.setAge(17);
            result = mapper.updateUser(user);
            Assert.assertEquals("更新 1行",
                    1, result);
            LOG.info("update User[999]: {}", mapper.getUser(user));

            result = mapper.delUser(user);
            Assert.assertEquals("删除 1行",
                    1, result);
            LOG.info("delete User[999]: {}", mapper.getUser(user));
        }
    }

    static {
        try {
            Reader reader = Resources.getResourceAsReader("framework/mybatis/annotation2/config-annotation2.xml");
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
        } catch (Exception e) {
            e.printStackTrace();
        }

        // Properties prop = new Properties();
        // try {
        //     prop.load(MybatisAnnotationConfigTest.class.getResourceAsStream("/framework/mybatis/db.properties"));
        // } catch (IOException e) {
        //     e.printStackTrace();
        // }
        //
        // PooledDataSourceFactory pooledDataSourceFactory = new PooledDataSourceFactory();
        // pooledDataSourceFactory.setProperties(prop);
        // DataSource dataSource = pooledDataSourceFactory.getDataSource();
        //
        // TransactionFactory transactionFactory = new JdbcTransactionFactory();
        // Environment environment = new Environment("development", transactionFactory, dataSource);
        // Configuration configuration = new Configuration(environment);
        //
        // // typeAliases
        // TypeAliasRegistry aliasRegistry = configuration.getTypeAliasRegistry();
        // aliasRegistry.registerAlias("UserVOAnnotation", UserVO.class);
        // aliasRegistry.registerAliases("framework.mybatis.annotation.entity");
        //
        // configuration.addMapper(UserMapper.class);
        // sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
    }

}
